package com.shms.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.SQLQuery;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.shms.bean.GroupOfBuilds;
import com.shms.dao.ShBuildDao;
import com.shms.entity.ShBuild;
import com.shms.entity.ShHome;
import com.shms.entity.ShPoint;
import com.shms.until.PageUntil;

/**
 * 文件名：ShBuildDaoImpl.java
 * 编写者：
 * 创建时间：2013-1-3
 * 简要描述：
 *
 */
public class ShBuildDaoImpl extends HibernateDaoSupport implements ShBuildDao {
	@SuppressWarnings({ "unchecked", "finally" })
	public List<ShBuild> getAllBno(){
		List<ShBuild> shb = new ArrayList<ShBuild>();
		try {
			String sql = "SELECT * FROM SH_BUILD";
			SQLQuery sqlQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(sql);
			shb = sqlQuery.addEntity(ShBuild.class).list();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			return shb;
		}
	}
	@SuppressWarnings("finally")
	public GroupOfBuilds queryBuilds(GroupOfBuilds goupOfBuilds) {
		GroupOfBuilds gOfBulidsRtn = new GroupOfBuilds();
		try {
			//查询数据总条数的SQL语句
			String sql = "SELECT COUNT(*) FROM SH_BUILD";
			SQLQuery sqlQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(sql);
			//获取总条数
			int allRow = Integer.parseInt(String.valueOf(sqlQuery.uniqueResult()));
			//获取总页数
			int totalPage = PageUntil.countTotalPage(goupOfBuilds.getPageSize(), allRow);
			//获取要查询显示的数据的页数
			int currentPage = PageUntil.countCurrentPage(goupOfBuilds.getCurrentPage(), totalPage);
			//获取要查询显示的数据的第一条的数字
			int currentRow = PageUntil.countCurrentRow(goupOfBuilds.getPageSize(), currentPage);
			
			sql = "SELECT * FROM SH_BUILD ORDER BY BNAME DESC";
			sqlQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(sql);
			//设置查询的第一条数据位置
			sqlQuery.setFirstResult(currentRow);
			//设置查询最大条数
			sqlQuery.setMaxResults(goupOfBuilds.getPageSize());
			@SuppressWarnings("unchecked")
			List<ShBuild> buildsListRtn = sqlQuery.addEntity(ShBuild.class).list();
			gOfBulidsRtn = new GroupOfBuilds(currentPage, totalPage, allRow, buildsListRtn); 
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			return gOfBulidsRtn;
		}
	}

	@SuppressWarnings("finally")
	@Override
	public boolean batchDelete(Integer[] ids) {
		boolean rtn = true;
		try {
			StringBuilder  sql  = new StringBuilder(); 
			sql.append("DELETE FROM SH_BUILD B WHERE B.BID IN (:ids) AND B.BID NOT IN ");
			sql.append("(SELECT B.BID FROM SH_BUILD B,SH_HOME H WHERE H.BID=B.BID)");
			SQLQuery sqlQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(sql.toString());
			sqlQuery.setParameterList("ids", ids);
			sqlQuery.executeUpdate();
		} catch (Exception ex) {
			ex.printStackTrace();
			rtn = false;
		} finally {
			return rtn;
		}
	}

	@SuppressWarnings("finally")
	@Override
	public boolean saveBuilds(ShBuild shBuild) {
		boolean rtn = true;
		try {
			this.getSessionFactory().getCurrentSession().save(shBuild);
		} catch (Exception ex) {
			ex.printStackTrace();
			rtn = false;
		} finally {
			return rtn;
		}
	}

	@SuppressWarnings("finally")
	@Override
	public boolean updateBuilds(ShBuild shBuild) {
		boolean rtn = true;
		try {
			this.getSessionFactory().getCurrentSession().update(shBuild);
		} catch (Exception ex) {
			ex.printStackTrace();
			rtn = false;
		} finally {
			return rtn;
		}
	}

	@SuppressWarnings("finally")
	@Override
	public ShBuild queryById(ShBuild shBuild) {
		ShBuild shBuildRtn = new ShBuild();
		try {
			String sql = "SELECT * FROM SH_BUILD WHERE BID = :bid";
			SQLQuery sqlQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(sql);
			sqlQuery.setProperties(shBuild);
			@SuppressWarnings("unchecked")
			List<ShBuild> listBuild = sqlQuery.addEntity(ShBuild.class).list();
			if (listBuild.size() == 1) {
				shBuildRtn = listBuild.get(0);
			}
		} catch (Exception ex) {
		ex.printStackTrace();
		} finally {
			return shBuildRtn;
		}
	}
	
	@SuppressWarnings({ "rawtypes", "finally" })
	public String queryBnameByBid(String bid){
		String bname = "";
		try {
			String sql = "SELECT BNAME FROM SH_BUILD WHERE BID ="+bid;
			SQLQuery sqlQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(sql); 
			List listBuild = sqlQuery.addEntity(ShBuild.class).list();
			if (listBuild.size() == 1) {
				bname = listBuild.get(0).toString();
			}
		} catch (Exception ex) {
		ex.printStackTrace();
		} finally { 
			return bname;
		}
	}
	@SuppressWarnings({ "unchecked", "finally" })
	@Override
	public boolean queryBuildsById(ShBuild shBuild) {
		boolean bbuild = true;
		List<ShHome> shc = new ArrayList<ShHome>();
		List<ShPoint> shp = new ArrayList<ShPoint>();
		try {
			String sql = "SELECT * FROM SH_BUILD B,SH_HOME H WHERE B.BID=H.BID AND B.BID = :bid";
			SQLQuery sqlQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(sql);
			sqlQuery.setProperties(shBuild);
			shc = sqlQuery.addEntity(ShHome.class).list();
			sql = "SELECT * FROM SH_BUILD B,SH_POINT P WHERE P.BID=B.BID AND B.BID = :bid";
			sqlQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(sql);
			sqlQuery.setProperties(shBuild);
			shp = sqlQuery.addEntity(ShPoint.class).list();
			
			if(shc.size()>0 || shp.size()>0){
				bbuild = false;
			}else{
				bbuild = true;
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			return bbuild;
		}
	}
}
